package cn.kbyue.doublecachedemo.service;

import cn.kbyue.doublecachedemo.aop.CacheType;
import cn.kbyue.doublecachedemo.aop.DoubleCache;
import cn.kbyue.doublecachedemo.entity.Order;
import org.springframework.stereotype.Service;

/**
 * 数据均模拟直接返回.
 */
@Service
public class CacheServiceImpl {

    @DoubleCache(cacheName = "order", key = "#id", type = CacheType.FULL)
    public Order getOrderById(Long id) {
        Order myOrder = new Order();
        myOrder.setId(1L);
        myOrder.setProductId(18263L);
        myOrder.setProductName("i7 7700");
        myOrder.setPrice(3000.0);
        myOrder.setAmount(1.0);
        if (id == 1L) {
            return myOrder;
        }
        return null;
    }

    @DoubleCache(cacheName = "order",key = "#order.id", type = CacheType.PUT)
    public Order updateOrder(Order order) {
        Order myOrder = new Order();
        myOrder.setId(1L);
        myOrder.setProductId(18263L);
        myOrder.setProductName("i7 7700K");
        myOrder.setPrice(3200.0);
        myOrder.setAmount(1.0);
        if (order.getId() == 1L) {
            return myOrder;
        }
        return null;
    }

    @DoubleCache(cacheName = "order",key = "#id", type = CacheType.DELETE)
    public void deleteOrder(Long id) {
        System.out.println(">> delete: " + id);
    }

}
